﻿Imports System.Data.SqlClient
Public Class frmCenter
    Dim frm As Form
    Private Sub frmCenter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = Now.Year - 10 To Now.Year + 2
            Me.cbxNam.Items.Add(i.ToString)
        Next
        Me.btnCty.Text = modKetNoi.aryConfig("CFG01")
        Me.cbxThang.Text = modKetNoi.aryConfig("Thang")
        Me.cbxNam.Text = modKetNoi.aryConfig("Nam")
        If modKetNoi.aryConfig("Locked") Then
            lblNotice.Visible = True
        End If
    End Sub

    Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
        If CapNhatKyHan() Then
            MsgBox("Thay đổi kỳ hạn làm việc thành công")
            modKetNoi.aryConfig("Locked") = checkIsLocked(cbxThang.Text, cbxNam.Text)
            If modKetNoi.aryConfig("Locked") Then
                lblNotice.Visible = True
                With DirectCast(Me.MdiParent, frmMain)
                    .stiLocked.Visible = True
                    .stiKyHan.Text = modKetNoi.aryConfig("Thang") & "/" & modKetNoi.aryConfig("Nam")
                End With
            Else
                lblNotice.Visible = False
                With DirectCast(Me.MdiParent, frmMain)
                    .stiLocked.Visible = False
                    .stiKyHan.Text = modKetNoi.aryConfig("Thang") & "/" & modKetNoi.aryConfig("Nam")
                End With
            End If
        End If
    End Sub

    Private Function CapNhatKyHan() As Boolean
        Dim strSql As String = ""
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Try
            myConn.Open()
            myComm.Connection = myConn
            strSql = "Update ThietLap set GiaTri = @GiaTri where TuKhoa = @TuKhoa"
            myComm.CommandText = strSql
            updateValue(Me.cbxThang.Text, "Thang", myComm)
            updateValue(Me.cbxNam.Text, "Nam", myComm)

        Catch ex As Exception
            Return False
        End Try
        myConn.Close()
        myConn.Dispose()
        Return True
    End Function

    Private Sub updateValue(ByVal value As String, ByVal key As String, ByVal mycomm As SqlCommand)
        value = value.Trim()
        If value <> "" Then
            mycomm.Parameters.Clear()
            mycomm.Parameters.AddWithValue("@TuKhoa", key)
            mycomm.Parameters.AddWithValue("@GiaTri", value)
            mycomm.ExecuteNonQuery()
            modKetNoi.aryConfig(key) = value
        End If
    End Sub

    Private Sub closeCurrentForm()
        If Not frm Is Nothing Then
            frm.Close()
        End If
    End Sub

    Private Sub btnCty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCty.Click
        closeCurrentForm()
        frm = New frmThietLap(Me)
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.Show()
        frm.MdiParent = Me.MdiParent
        Me.btnCty.Text = modKetNoi.aryConfig("CFG01")
    End Sub

    Private Sub lblTaiKhoan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTaiKhoan.Click
        closeCurrentForm()
        frm = New frmTaiKhoanDs()
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.Show()
        frm.MdiParent = Me.MdiParent
    End Sub

    Private Sub lblHangHoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblHangHoa.Click
        closeCurrentForm()
        frm = New frmHangHoaDs()
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.Show()
        frm.MdiParent = Me.MdiParent
    End Sub

    Private Sub lblKhachHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblKhachHang.Click
        closeCurrentForm()
        frm = New frmKhachHangDs()
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.Show()
        frm.MdiParent = Me.MdiParent
    End Sub

    Private Sub lblSoCai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblSoCai.Click
        closeCurrentForm()
        frm = New frmSoCai()
        frm.StartPosition = FormStartPosition.CenterScreen
        frm.Show()
        frm.MdiParent = Me.MdiParent
    End Sub

    Private Sub lblChiTiet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblChiTiet.Click

    End Sub

    Private Sub lblTongQuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTongQuat.Click

    End Sub

    Private Sub lblXuatKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblXuatKho.Click

    End Sub

    Private Sub lblNhapKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblNhapKho.Click

    End Sub
End Class